﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Models;
using BLL;
using System.Net;

namespace Dryricenet.houTai
{
    public partial class userAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Xlk();
            }
        }
        //下拉框
        public void Xlk()
        {
            Type.DataSource = userType.liXlkBLL();
            Type.DataTextField = "Name";
            Type.DataValueField = "TypeID";
            Type.DataBind();
        }
        //用户或管理员添加
        protected void Add_Button_Click(object sender, EventArgs e)
        {
            if (Empty() == true)
            {
                string sex = null;
                if (rdbNan.Checked)
                {
                    sex = "男";
                }
                else
                {
                    sex = "女";
                }
                User us = new User
                {
                    UserName = txtName.Text.Trim(),
                    Pwd = txtPwd.Text.Trim(),
                    UserTypeID = Type.SelectedIndex + 1,
                    Sex = sex,
                    Age = Convert.ToInt32(txtAge.Text.Trim()),
                    Phone = txtPhone.Text.Trim(),
                };
                string name = Session["UserName"].ToString();
                System.Data.DataTable dt = userBLL.selectNameBll(name);
                DataRow dr = dt.Rows[0];//第一条数据
                int userID = Convert.ToInt32(dr["UserID"].ToString());
                if (Type.SelectedIndex + 1 == 2)
                {
                    if (userID == 1)
                    {
                        if (userBLL.UserCF(txtName.Text.Trim()) == null)
                        {
                            Log l = new Log();
                            l.OperTime = DateTime.Now;
                            l.OperPerson = name;
                            l.Type = "添加用户";
                            l.State = "添加成功";
                            l.ComputerName = Dns.GetHostName().ToUpperInvariant();
                            l.System = Systems.GetOSNameByUserAgent(Environment.OSVersion.ToString());
                            l.UserID = userID;
                            LogBLL.LogAddBLL(l);
                           
                            if (userBLL.addUserBLL(us))
                            {
                                
                                Response.Write("<script>alert('添加成功！')</script>");
                            }
                            else
                            {
                                Response.Write("<script>alert('添加失败！')</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('此用户已存在！请更改用户名。')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('只有超级管理员才能添加管理员！您的权限不够！')</script>");
                    }
                }
                else {
                    if (userBLL.UserCF(txtName.Text.Trim()) == null)
                    {
                        if (userBLL.addUserBLL(us))
                        {
                            Response.Write("<script>alert('添加成功！')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('添加失败！')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('此用户已存在！请更改用户名。')</script>");
                    }
                }
               
               
            }
            
        }
        public bool Empty() {
            if (txtName.Text.Trim() != "")
            {
                
                if (txtPwd.Text.Trim() != "")
                {
                    
                    if (txtPhone.Text.Trim() != "")
                    {
                        if (txtAge.Text.Trim() != "")
                        {
                            int age = Convert.ToInt32(txtAge.Text.Trim());
                            if (age > 10 && age < 108)
                            {
                                string sj = txtPhone.Text.Trim();
                                if (Validation.IsHandset(sj) == true)
                                {
                                    return true;
                                }
                                else
                                {
                                    Response.Write("<script>alert('手机号输入不正确！')</script>");
                                    return false;
                                }
                            }
                            else
                            {
                                Response.Write("<script>alert('年龄输入不合法！')</script>");
                                return false;
                            }
                        }
                        else
                        {
                           
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else {
                return false;
            }
        }
    }
}